package com.aluka.nirvana.framework.security.utils;

import com.aluka.nirvana.framework.security.principal.BaseUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author gongli
 * @since 2020/4/16
 */  
public class SecurityUtils {

    public static BaseUserDetails getUserDetails(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null){
            Object principal = authentication.getPrincipal();
            if(principal instanceof BaseUserDetails){
                return (BaseUserDetails) principal;
            }
        }

        return new BaseUserDetails();
    }

    public static String getUserId(){
        return getUserDetails().getUserId();
    }

    public static String getUsername(){
        return getUserDetails().getUsername();
    }
}
